var utils = require("../../utils/utils");
var ocrGml = require("../../utils/ocrGml");
var appUtils = require("../../utils/app");
var readArticle = require("../appCommon/readArticle");
var pointMap = {
  免费领取关闭1: {
    points: [
      // prettier-ignore
      958, 173, 1035, 250,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  免费获取关闭1: {
    points: [
      // prettier-ignore
      980, 195, 1013, 228,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  奖励将于关闭1: {
    points: [
      // prettier-ignore
      964, 171, 1008, 215,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  奖励将于关闭2: {
    points: [
      // prettier-ignore
      974, 148, 998, 175,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  twoNumber关闭2: {
    points: [
      // prettier-ignore
      975, 176, 1008, 209,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  后可领取奖励关闭2: {
    points: [
      // prettier-ignore
      970, 125, 1030, 190,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
};

var ad = {
  watchAd() {
    sleep(5 * 1000);
    var adType = this.getAdType();
    this.watchAdType(adType);
  },
  watchAdType(type) {
    var map = {
      1: "后可领取奖励",
      2: "跳过",
      3: "秒后得",
      4: "直播间观看时长",
      5: "立即获取",
      6: "奖励将于",
      7: "30s",
    };
    console.log("watchAdType:", type, "/", map[type]);
    if (type == 7) {
      this.watchAdType7(utils.getRandom(50, 50 + 3));
    }
    if (type == 6) {
      this.watchAdType6(utils.getRandom(50, 50 + 3));
    }
    if (type == 5) {
      this.watchAdType5(utils.getRandom(50, 50 + 3));
    }
    if (type == 4) {
      this.watchAdType4(utils.getRandom(50, 50 + 3));
    }
    if (type == 3) {
      this.watchAdType3();
    }
    if (type == 2) {
      this.watchAdType2();
    }
    if (type == 1) {
      this.watchAdType1(utils.getRandom(50, 50 + 3));
    }
  },
  getAdType() {
    for (var index = 0; index < 5; index++) {
      var content = /^\d{2}s$/;
      if (utils.existsTextMatches(content)) {
        return 7;
      }
      var content = "奖励将于";
      if (utils.existsTextStartsWith(content)) {
        return 6;
      }
      var content = "立即获取";
      if (utils.existsTextStartsWith(content)) {
        return 5;
      }
      var content = "秒后得";
      if (utils.existsTextEndsWith(content)) {
        return 3;
      }
      var content = "跳过";
      if (utils.existsTextEndsWith(content)) {
        return 2;
      }
      var content = "后可领取奖励";
      if (utils.existsTextEndsWith(content)) {
        return 1;
      }
      sleep(2 * 1000);
    }
  },
  type7Back(type) {
    if (type == 1) {
      appUtils.openApp("爆爽短剧");
      sleep(utils.getRandom(1.5, 2.5) * 1000);
      utils.clickPointMap(pointMap["免费领取关闭1"]);
    } else if (type == 2) {
    } else {
      sleep(utils.getRandom(1.5, 2.5) * 1000);
      utils.clickPointMap(pointMap["twoNumber关闭2"]);
    }
    sleep(utils.getRandom(1.5, 2.5, 1000));
    var content = "开心收下";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    }
  },
  watchAdType7() {
    console.log("watchAdType5:");
    var startTime = Date.now();
    var span = utils.getRandom(50, 60);
    var count = 0;
    while (true) {
      sleep(utils.getRandom(2.5, 4) * 1000);
      var content = "应用商店";
      var UIText = utils.existsTextEndsWith(content);
      if (UIText) {
        this.type7Back(1);
        break;
      }
      var content = "下载次数";
      var UIText = utils.existsTextEndsWith(content);
      if (UIText) {
        this.type7Back(1);
        break;
      }
      var content = "跳过";
      var UIText = utils.clickTextEndsWith(content);
      if (UIText) {
        this.type7Back(2);
        break;
      }
      count++;
      var now = Date.now();
      var hasWatch = (now - startTime) / 1000;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      if (hasWatch >= span) {
        console.log("最长广告时间已达，强制结束");
        console.log(currentPackage());
        this.type7Back();
        break;
      }
    }
  },
  type6Back() {
    var content = "恭喜";
    if (utils.existsTextStartsWith(content)) {
      utils.clickPointMap(pointMap["奖励将于关闭1"]);
      sleep(utils.getRandom(2.5, 8.5, 1000));
      utils.clickPointMap(pointMap["奖励将于关闭2"]);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    } else {
      sleep(utils.getRandom(2.5, 8.5, 1000));
      utils.clickPointMap(pointMap["奖励将于关闭2"]);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    }
    var content = "开心收下";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    }
  },
  watchAdType6() {
    console.log("watchAdType6:");
    var startTime = Date.now();
    var span = utils.getRandom(50, 60);
    var count = 0;
    var hasCheck_buyongle = false;
    while (true) {
      sleep(utils.getRandom(1.5, 3) * 1000);
      var content = "恭喜获得";
      var UIText = utils.existsTextStartsWith(content);
      if (UIText) {
        this.type6Back();
        break;
      }
      var content = "不用了";
      var UIText = utils.existsTextEndsWith(content);
      if (UIText) {
        utils.clickUIText(UIText);
        hasCheck_buyongle = true;
      }
      var content = "放弃福利";
      var UIText = utils.existsTextEndsWith(content);
      if (UIText) {
        utils.clickUIText(UIText);
      }
      count++;
      var now = Date.now();
      var hasWatch = (now - startTime) / 1000;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      if (hasWatch > 10 && !hasCheck_buyongle) {
        back();
      }
      if (hasWatch >= span) {
        console.log("最长广告时间已达，强制结束");
        console.log(currentPackage());
        this.type6Back();
        break;
      }
    }
  },
  type5Back(openApp) {
    if (openApp) {
      appUtils.openApp("爆爽短剧");
      sleep(utils.getRandom(1.5, 2.5) * 1000);
      utils.clickPointMap(pointMap["免费领取关闭1"]);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    } else {
      sleep(utils.getRandom(1.5, 2.5) * 1000);
      utils.clickPointMap(pointMap["免费获取关闭1"]);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    }
    var content = "开心收下";
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(1.5, 2.5, 1000));
    }
  },
  watchAdType5() {
    console.log("watchAdType5:");
    var startTime = Date.now();
    var span = utils.getRandom(50, 60);
    var count = 0;
    while (true) {
      sleep(utils.getRandom(2.5, 4) * 1000);
      var content = "应用商店";
      var UIText = utils.existsTextEndsWith(content);
      if (UIText) {
        this.type5Back(true);
        break;
      }
      var content = "下载次数";
      var UIText = utils.existsTextEndsWith(content);
      if (UIText) {
        this.type5Back(true);
        break;
      }
      count++;
      var now = Date.now();
      var hasWatch = (now - startTime) / 1000;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      if (hasWatch >= span) {
        console.log("最长广告时间已达，强制结束");
        console.log(currentPackage());
        this.type5Back();
        break;
      }
    }
  },
  checkBack() {
    for (var index = 0; index < 5; index++) {
      sleep(utils.getRandom(1.5, 2.5, 10000));
      var content = "退出";
      if (utils.clickTextEndsWith(content)) {
        return true;
      }
      var content = "后可领奖励";
      if (utils.existsTextEndsWith(content)) {
        return true;
      }
      var content = "广告";
      if (utils.existsTextStartsWith(content)) {
        return true;
      }
      var content = "领取成功";
      if (utils.existsTextStartsWith(content)) {
        return true;
      }
      back();
    }
  },
  watchAdType3() {
    var span = utils.getRandom(32, 35);
    readArticle(span, 3);
    this.back();
  },
  watchAdType2() {
    var span = utils.getRandom(65, 70);
    var startTime = Date.now();
    var count = 0;
    var checkActivity = false;

    while (true) {
      var hasWatch = (Date.now() - startTime) / 1000;
      if (hasWatch > span) {
        console.log("最长广告时间已达，强制结束");
        console.log(currentPackage());
        break;
      }
      sleep(5 * 1000);
      count++;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      var content = "关闭";
      if (utils.clickTextEndsWith(content)) {
        this.back(true);
        return;
      }
      if (!checkActivity) {
        if (hasWatch > 15) {
          var content = ["后过期", "后结束"];
          var ocrItem = ocrGml.captureExistOne(content);
          if (ocrItem) {
            span = span + 30;
            checkActivity = true;
          }
        }
      }
    }
    var content = "跳过";
    if (utils.clickTextEndsWith(content)) {
      this.back(true);
      return;
    }
    var content = "关闭";
    if (utils.clickTextEndsWith(content)) {
      this.back(true);
      return;
    }
  },
  watchAdType1(span) {
    var startTime = Date.now();
    var count = 0;
    while (true) {
      var hasWatch = (Date.now() - startTime) / 1000;
      if (hasWatch > span) {
        console.log("最长广告时间已达，强制结束");
        console.log(currentPackage());
        break;
      }
      sleep(2 * 1000);
      count++;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      var content = "跳过";
      if (
        utils.clickTextStartsWith(content) ||
        utils.clickTextEndsWith(content)
      ) {
        this.type1Back(true);
        return;
      }
    }
    this.type1Back();
  },
  type1Back(notNeedBack) {
    if (!notNeedBack) {
      utils.clickPointMap(pointMap["后可领取奖励关闭2"]);
    }
    sleep(utils.getRandom(2, 3, 1000));
    var content = "开心收下";
    if (utils.clickTextStartsWith(content)) {
      sleep(utils.getRandom(2.5, 3, 1000));
      return;
    }
  },
  back(notNeedBack) {
    if (!notNeedBack) {
      back();
    }
    sleep(utils.getRandom(2, 3, 1000));
    var content = "开心收下";
    if (utils.clickTextStartsWith(content)) {
      sleep(utils.getRandom(2.5, 3, 1000));
      return;
    }
  },
};
exports.ad = ad;
exports.pointMap = pointMap;
